package com.budwk.app.sqfwV2.generator.shawenTown.pdf;

import com.budwk.app.sqfw.constants.CommonConstants;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.PdfGenerateParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.BasePdfGenerator;
import com.budwk.app.sqfwV2.component.ShawenTownComponent;
import com.budwk.app.sqfwV2.component.SignImageComponent;
import com.budwk.app.sqfwV2.dto.dto.dayingRoad.ProblemParamDTO;
import com.budwk.app.sqfwV2.dto.entity.SqfwProblemInfo;
import com.budwk.app.sqfwV2.dto.shawenTown.SWTownProblemVo;
import com.budwk.app.sqfwV2.dto.shawenTown.ShawenTownSafetyPdfDTO;
import com.budwk.app.sqfwV2.dto.shawenTown.ShawenTownSafetyProblemVo;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @author 电动自行车排查整治表
 * @since 2024-03-11
 */
public class ElectricBicyclesWordGenerator extends BasePdfGenerator{

    /**
    * 是否有电动自行车=是 时生成表格
    * @return 结果
    */
    @Override
    public boolean pdfCreateConditionCheck(PdfGenerateParam pdfGenerateParam) {
        ShawenTownSafetyPdfDTO pdfDTO = ShawenTownComponent.getShawenTownSafetyPdfDTO(pdfGenerateParam);
        ShawenTownSafetyProblemVo s = pdfDTO.getElectricBicycle();
        if (s != null){
            // 是否有电动自行车=是
            return CommonConstants.TRUE.equals(s.getKey());
        }
        return false;
    }

    @Override
    public String getTemplatePath(PdfGenerateParam pdfGenerateParam) {
        return "sqfwV2/shawenTown/electricBicyclesWord.docx";
    }

    @Override
    public void customerPopularPdfParam(Map<String, Object> paramMap, PdfGenerateParam pdfGenerateParam) {
        // 在这里对一些数据进行填充 进行复杂的业务代码逻辑
        ShawenTownSafetyPdfDTO pdfDTO = ShawenTownComponent.getShawenTownSafetyPdfDTO(pdfGenerateParam);
        // 构建参数
        ShawenTownSafetyProblemVo electricBicycle = pdfDTO.getElectricBicycle();
        buildParam(paramMap, electricBicycle.getParams(), pdfGenerateParam, pdfDTO);
    }

    private void buildParam(Map<String, Object> paramMap, List<SWTownProblemVo> list, PdfGenerateParam pdfGenerateParam, ShawenTownSafetyPdfDTO pdfDTO) {
        if (list == null || list.size() == 0) {
            list = new ArrayList<>();
        }
        // 获取所有问题的默认值
        List<SqfwProblemInfo> values = ShawenTownComponent.getElecDefaultValues();
        for (SqfwProblemInfo problemEnum : values) {
            Optional<SWTownProblemVo> optioanl = list.stream().filter(e -> problemEnum.getFields().equals(e.getFields())).findFirst();
            if (optioanl.isEmpty()) {
                paramMap.put(problemEnum.getFields(), StringUtils.EMPTY);
                continue;
            }
            ProblemParamDTO s = optioanl.get();
            paramMap.put(s.getFields(), s.getKey().equals(CommonConstants.TRUE) ? "是" : "否");
        }
        // 获取罚款金额
        SWTownProblemVo s = ShawenTownComponent.getElecProblemByNumber(ShawenTownComponent.getElecFourDefaults(), "3");
        BigDecimal penaltyAmount = ShawenTownComponent.getPenaltyAmount(list, s);
        paramMap.put("fineOrNot", penaltyAmount);
        // 签名
        SignImageComponent.signWord(paramMap, 5.0F, pdfDTO.getCheckedUnitLeader(),
                pdfGenerateParam.getCreateUserCommonParam().getCreateUserName(), "checkedUnitLeader", "checkedUnitLeaderStr");
        // 巡查时间
        ShawenTownComponent.formatFillDate(pdfDTO, paramMap);
    }
}














